package terra.model.object;

import terra.Config;
import terra.definitions.NpcDef;
import terra.managers.DefManager;
import terra.managers.MapManager;
import terra.model.World;
import terra.model.object.interfaces.Interactable;
import terra.model.object.map.AbstractMapObject;
import terra.model.object.map.MapObject;
import terra.network.packet.server.StaticInfo;
import terra.util.Util;

public class Flora extends StaticObject implements Interactable {

	private static final long serialVersionUID = -8110870993555720968L;
	boolean underwater = false;

	public Flora() {
	}

	public Flora(int objectId) {
		super(objectId);
	}
	
	public Flora(NpcDef d) {
		super();
		underwater = d.getBoolProperty("underwater");
	}
	
	public Flora(int objectId, int defId) {
		super(objectId,defId);
	}

	public void applyDef() {
		NpcDef d = DefManager.npcDefs.get(getDefId());
		//super.applyDef(d);
		underwater = d.getBoolProperty("underwater");
		setImage(d.getStringProperty("image"));
		setName(d.getStringProperty("name"));
		setWidth(d.getIntProperty("width") == 0 ? Config.tileSize : d
				.getIntProperty("width"));
		setHeight(d.getIntProperty("height") == 0 ? Config.tileSize : d
				.getIntProperty("height"));
	}

	@Override
	public void update(int delta) {
		AbstractMapObject map = MapManager.getInstance().getMaps()
				.get(getMapIndex()).getMap()[getGroundTileX()][getGroundTileY() - 1];
		if (underwater) {
			if (map != null
					&& ((map.getType() == MapObject.TYPE_WATER && map.getMass() <= 1f) || map
							.getType() != MapObject.TYPE_WATER) || map == null) {
				World.getInstance().removeObject(this);
				removeObject();
				return;
			}
		}
		super.update(delta);
	}

	@Override
	public void onClick(int button, Player player) {
		if (distanceTo(player) < Config.tileSize * 2 && !isRemove()) {
			Util.doItemDrop(DefManager.npcDefs.get(getDefId()).drop,
					(int) getX(), (int) getY(), getGroundTileX(),
					getGroundTileY(), getMapIndex());
			World.getInstance().removeObject(this);
			removeObject();
		}
	}

	public StaticInfo getInfo() {
		StaticInfo info = new StaticInfo();
		info.oId = getObjectId();
		info.id = getDefId();
		info.x = getX();
		info.y = getY();
		info.type = StaticInfo.FLORA;
		return info;
	}

}
